﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;

namespace NDK.Module.DAL
{
    class PathServices
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DataSavePath");
            strSql.Append(" where name=@name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String,50)};
            parameters[0].Value = name;

            return SQLiteUtils.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(PathModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DataSavePath(");
            strSql.Append("name,path)");
            strSql.Append(" values (");
            strSql.Append("@name,@path)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String,100),
					new SQLiteParameter("@path", DbType.String)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.path;

            SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PathModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DataSavePath set ");
            strSql.Append("path=@path");
            strSql.Append(" where name=@name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@path", DbType.String),
					new SQLiteParameter("@name", DbType.String,100)};
            parameters[0].Value = model.path;
            parameters[1].Value = model.name;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DataSavePath ");
            strSql.Append(" where name=@name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String,50)};
            parameters[0].Value = name;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string namelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DataSavePath ");
            strSql.Append(" where name in (" + namelist + ")  ");
            int rows = SQLiteUtils.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PathModel GetModel(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select name,path from DataSavePath ");
            strSql.Append(" where name=@name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String,50)};
            parameters[0].Value = name;

            PathModel model = new PathModel();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.name = ds.Tables[0].Rows[0]["name"].ToString();
                model.path = ds.Tables[0].Rows[0]["path"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select name,path ");
            strSql.Append(" FROM DataSavePath ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteUtils.Query(strSql.ToString());
        }


        #endregion  Method
    }
}
